package com.mzy.xmyx.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mzy.xmyx.common.util.MD5;
import com.mzy.xmyx.acl.mapper.AdminMapper;
import com.mzy.xmyx.acl.service.AdminService;
import com.mzy.xmyx.model.acl.Admin;
import com.mzy.xmyx.vo.acl.AdminQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public IPage<Admin> selectPage(Integer current, Integer limit, AdminQueryVo adminQueryVo) {

        String name = adminQueryVo.getName();
        String username = adminQueryVo.getUsername();
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like(Admin::getName, name);
        }
        if (!StringUtils.isEmpty(username)) {
            queryWrapper.like(Admin::getUsername, username);
        }

        Page<Admin> page = new Page<>(current, limit);
        IPage<Admin> adminPage = adminMapper.selectPage(page, queryWrapper);
        return adminPage;
    }

    @Override
    public boolean save(Admin admin) {
        String password = MD5.encrypt(admin.getPassword());
        admin.setPassword(password);
        int row = adminMapper.insert(admin);
        return row > 0;
    }



}
